package com.bjsxt.ssm.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;




import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bjsxt.ssm.Constant;
import com.bjsxt.ssm.bean.TbResRo;
import com.bjsxt.ssm.bean.TbResource;
import com.bjsxt.ssm.bean.TbRole;
import com.bjsxt.ssm.service.TbResRoService;
import com.bjsxt.ssm.service.TbResourceService;
import com.bjsxt.ssm.service.TbRoleService;
import com.bjsxt.ssm.view.Rst;

@Controller
@RequestMapping("/admin/role")
public class RoleController {
	
	@Autowired
	private TbRoleService tbRoleService;
	
	@Autowired
	private TbResourceService tbResourceService;
	
	@Autowired
	private TbResRoService tbResRoService;
	
	@RequestMapping("/list.do")
	public String  list(ModelMap view){
		int totalNumber = tbRoleService.queryCount();
		view.addAttribute("totalNumber", totalNumber);
		return "role/list";
	}
	@RequestMapping("/add.do")
	public String add(){
		return "role/add";
	}
	
	@RequestMapping("/edit/{roleId}.do")
	public String edit(@PathVariable Integer roleId, ModelMap view){
		TbRole tbRole=tbRoleService.selectTbRole(roleId);
		view.addAttribute("tbRole", tbRole);
		return "role/edit";
	}
	
	/**设置资源*/
	@RequestMapping("/role/{roleIds}.do")
	public String role(@PathVariable Integer[] roleIds,ModelMap view){
		List<TbRole> tbRoles=new ArrayList<TbRole>();
		for (Integer roleId : roleIds) {
			TbRole tbRole=tbRoleService.selectTbRole(roleId);
			tbRoles.add(tbRole);
		}
		List<TbResource> resources=tbResourceService.queryAll(); 
		view.addAttribute("roles",tbRoles);
		view.addAttribute("resources",resources);
		return "role/role";
	}
	
	@RequestMapping("/details/{roleId}.do")
	public String details(@PathVariable Integer roleId,
			ModelMap view){
		
		 TbRole tbRole=tbRoleService.selectTbRole(roleId);
		List<TbResource> resources=new ArrayList<TbResource>();

		 List<TbResRo> tbResRo=tbResRoService.selectTbResById(roleId);
		 if(CollectionUtils.isNotEmpty(tbResRo)){
				for(int n=0;n<tbResRo.size();n++){
					TbResRo resRo=tbResRo.get(n);
					TbResource resource=tbResourceService.selectTbResource(resRo.getResourceId());
					if(null!=resource){
						resources.add(resource);
					}
				}
			}
		 view.addAttribute("tbRole",tbRole);
		 view.addAttribute("resources",resources);
		return "role/details";
	}
	
	@RequestMapping("/save.do")
	@ResponseBody
	public Rst save(TbRole role){
		Rst rst=new Rst();
		if(null==role){
			rst.setCode(Constant.ERROR);
			rst.setMessage("用户信息不能为空");
			return rst;
		}
		
		int  roleId=this.tbRoleService.saveTbRole(role);
		if(roleId>0){
			rst.setCode(Constant.SUCCESS);
			rst.setMessage("新增成功");
		} else {
			rst.setCode(Constant.ERROR);
			rst.setMessage("新增失败");
		}
		return rst;
	}
	
	@RequestMapping("/pagination.do")
	@ResponseBody
	public Rst pagination(Integer pageSize,
			@RequestParam(value = "pageNumber") Integer page) {
		Rst rst=new Rst();
		if (0 >= pageSize || 0 >= page) {
			rst.setCode(Constant.ERROR);
			rst.setMessage("参数错误");
			return rst;
		}
		List<TbRole> list=tbRoleService.pagination(pageSize, page);
		rst.setCode(Constant.SUCCESS);
		rst.setMessage("获取成功");
		if (CollectionUtils.isNotEmpty(list)) {
			rst.setData(list);
		} else {
			rst.setData(new ArrayList<>());
		}
		return rst;
	}
	
	@RequestMapping("/del.do")
	@ResponseBody
	public Rst del(Integer[] roleIds){
		Rst rst=new Rst();

		if (ArrayUtils.isEmpty(roleIds)) {
			rst.setCode(Constant.ERROR);
			rst.setMessage("参数错误,删除失败");
		}
		int state = tbRoleService.deleteTbRole(roleIds);

		if (0 < state) {
			rst.setCode(Constant.SUCCESS);
			rst.setMessage("成功删除【" + state + "】条数据");
		} else {
			rst.setCode(Constant.ERROR);
			rst.setMessage("删除失败");
		}
		return rst;
	}
	
	
	@RequestMapping("/update.do")
	@ResponseBody
	public Rst update(TbRole tbRole){
		Rst rst=new Rst();
		if (null == tbRole) {
			rst.setCode(Constant.ERROR);
			rst.setMessage("用户信息不能为空");
			return rst;
		}

		int state = tbRoleService.updateTbRole(tbRole);
		if (0 < state) {
			rst.setCode(Constant.SUCCESS);
			rst.setMessage("修改成功");
		} else {
			rst.setCode(Constant.ERROR);
			rst.setMessage("修改失败");
		}
		
		return rst;
	}
	
	@RequestMapping("/role/bangding.do")
	@ResponseBody
	public Rst bangding(Integer[] roleIds,Integer[]resourceIds,String oper){
		Rst rst=new Rst();

		if (ArrayUtils.isEmpty(roleIds)) {
			rst.setCode(Constant.ERROR);
			rst.setMessage("参数错误,绑定失败");
			return rst;
		}
		
		if(StringUtils.isBlank(oper)){
			rst.setCode(Constant.ERROR);
			rst.setMessage("参数错误,绑定失败");
			return rst;
		}
		int state = 0;
		/**追加*/
		if("append".equalsIgnoreCase(oper)){
			state = tbResRoService.useAppendRole(roleIds,resourceIds) ;
		}
		/**覆盖*/
		else if("overwrite".equalsIgnoreCase(oper)){
			state = tbResRoService.useOverWriteRole(roleIds,resourceIds) ;
		}else{
			rst.setCode(Constant.ERROR);
			rst.setMessage("参数错误,绑定失败");
			return rst;
		}
		
		
		if (0 < state) {
			rst.setCode(Constant.SUCCESS);
			rst.setMessage("成功绑定【" + state + "】条数据");
		} else {
			rst.setCode(Constant.ERROR);
			rst.setMessage("绑定失败");
		}
		return rst;
	}
}
